package ${controllerPkg};

import com.baomidou.mybatisplus.core.metadata.IPage;
import ${poPkg}.${poName};
import ${qoPkg}.${qoName};
import ${toPkg}.IdsTo;
import ${toPkg}.ResultTo;
import ${servicePkg}.${serviceName};
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("${alias}")
@Api(tags = "${alias}")
public class ${controllerName} extends BaseController {
    @Autowired
    private ${serviceName} ${alias}Service;
    /**
     * 保存到数据库。如果传入的对象有ID，则更新对象；没有ID则新增对象
     * @param ${alias} 保存的对象
     */
    @PostMapping("/save")
    public void save${aliasCapFirst}(@RequestBody @NotNull @Validated ${poName} ${alias}) {
        if (null == ${alias}.getId()) {
            ${alias}Service.add${aliasCapFirst}(${alias});
        } else {
            ${alias}Service.update${aliasCapFirst}(${alias});
        }
    }

    /**
     * 根据ID删除记录
     * @param id
     */
    @PostMapping("/del/{id}")
    public void del(@PathVariable("id") long id) {
        ${alias}Service.del${aliasCapFirst}(id);
    }
    /**
     * 根据ID获取单个对象信息
     * @param id
     */
    @GetMapping("/${alias}/{id}}")
    public ${poName} ${alias}(@PathVariable("id") long id) {
        return ${alias}Service.get${aliasCapFirst}(id);
    }

    /**
     * 根据条件查询记录并分页
     * @param ${alias}Qo 查询对象
     */
    @GetMapping("/${alias}s")
    public IPage<${poName}> ${alias}s(${qoName} ${alias}Qo) {
        return ${alias}Service.get${aliasCapFirst}AndPage(${alias}Qo);
    }

    /**
     * 根据条件查询记录不分页
     * @param ${alias}Qo 查询对象
     */
    @GetMapping("/${alias}List")
    public List<${poName}> ${alias}List(${qoName} ${alias}Qo) {
        return ${alias}Service.get${aliasCapFirst}s(${alias}Qo);
    }

    /**
     * 根据ID集合批量删除数据
     * @param idsTo ID集合对象
     */
    @PostMapping("/delBatch")
    public void delBatch(@RequestBody IdsTo idsTo) {
        Assert.isTrue(null != idsTo && null != idsTo.getIds(), ResultTo.getCodeAndMsg(ResultTo.ResultCode.DATA_NO, "请选择要删除的记录"));
        ${alias}Service.del${aliasCapFirst}ByIds(idsTo.getIds());
    }
}
